<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>

    var smallest = function (nums, queries) {
      const compareNum = (a, b) => {
        if (a[0] < b[0]) {
          return -1;
        } else if (a[0] === b[0]) {
          return 0
        }
        return 1;
      }
      const res = [];
      for (let q of queries) {
        const newPair = [];
        nums.forEach((item, index) => {
          const n = item.length;
          newPair.push([item.slice(n - q[1], n), index])
        })
        newPair.sort(compareNum);
        res.push(newPair[q[0] - 1][1]);
      }
      return res;
    };
    console.log(smallest(nums = ["102", "473", "251", "814"], queries = [[1, 1], [2, 3], [4, 2], [1, 2]]))
  </script>
</body>

</html>